package cn.edu.swust.adressbook.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.List;

import cn.edu.swust.adressbook.bean.Contact;
import cn.edu.swust.adressbook.view.ContactItemView;


public class ContactItemAdapter extends BaseAdapter {

    private List<Contact> mContacts;
    private Context mContext;
    private boolean mIsNeedCheck = false;


    public ContactItemAdapter(Context context) {
        super();
        mContext = context;
    }

    public void setContacts(List<Contact> contacts) {
        mContacts = contacts;
    }

    public void setNeedCheck(boolean needCheck) {
        mIsNeedCheck = needCheck;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public int getCount() {
        return mContacts.size();
    }

    @Override
    public Object getItem(int i) {
        return mContacts.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }


    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ContactItemView contactItemView;
        if(view == null){
            contactItemView = new ContactItemView(mContext, null);
        } else {
            contactItemView = (ContactItemView) view;
        }
        if (i>0){

            char pinyin = mContacts.get(i).getPinyin().charAt(0);
            if(pinyin != mContacts.get(i - 1).getPinyin().charAt(0)){
                contactItemView.setHeadTextVisible(true, pinyin);
            } else {
                contactItemView.setHeadTextVisible(false, ' ');
            }
        } else {
            contactItemView.setHeadTextVisible(false, ' ');
        }
        contactItemView.setName(mContacts.get(i).getName());
        contactItemView.setCheckBoxVisibility(mIsNeedCheck);

        return contactItemView;
    }
}
